home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / stdio / RCS / stdio.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  44.9 KB  |  1,744 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.3 srv027:1.3 srv026:1.3 srv024:1.3 srv021:1.3 srv018:1.3 srv014:1.3 srv010:1.3 srv008:1.3 srv007:1.3 srv006:1.3 srv004:1.3;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.03.23.15.12.06;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     91.12.12.22.43.51;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     91.12.12.22.42.25;  author kupfer;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @Test program for standard I/O.
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Re-enable tests that require /dev/console to be working.
  33. @
  34. text
  35. @/* 
  36.  * stdio.c --
  37.  *
  38.  *    This file contains a program that exercises the stdio
  39.  *    library facilities.  Invoke it with no parameters;  it
  40.  *    will print messages on stderr for any problems it detects
  41.  *    with the stdio procedures.
  42.  *
  43.  * Copyright 1988 Regents of the University of California
  44.  * Permission to use, copy, modify, and distribute this
  45.  * software and its documentation for any purpose and without
  46.  * fee is hereby granted, provided that the above copyright
  47.  * notice appear in all copies.  The University of California
  48.  * makes no representations about the suitability of this
  49.  * software for any purpose.  It is provided "as is" without
  50.  * express or implied warranty.
  51.  */
  52.  
  53. #ifndef lint
  54. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/stdio/RCS/stdio.c,v 1.2 91/12/12 22:43:51 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)";
  55. #endif not lint
  56.  
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <sys/file.h>
  60. #include <sys/wait.h>
  61. #include <signal.h>
  62. #include <varargs.h>
  63.  
  64. #ifdef sun3
  65. #define BIGENDIAN
  66. #endif
  67. #ifdef sun4
  68. #define BIGENDIAN
  69. #endif
  70. #ifdef ds3100
  71. #define LITTLEENDIAN
  72. #endif
  73.  
  74. #if 1
  75. #define error(string) \
  76.     fprintf(stderr, string); \
  77.     exit(1);
  78. #else
  79. #define error(string) \
  80.     fprintf(stderr, string);
  81. #endif
  82.  
  83. /*
  84.  *----------------------------------------------------------------------
  85.  *
  86.  * CheckFile --
  87.  *
  88.  *    Utility procedure to make sure that a given file contains a
  89.  *    given value.  Aborts with error if it doesn't.
  90.  *
  91.  * Results:
  92.  *    None.
  93.  *
  94.  * Side effects:
  95.  *    None.
  96.  *
  97.  *----------------------------------------------------------------------
  98.  */
  99.  
  100. void
  101. CheckFile(name, value, errMsg)
  102.     char *name;            /* Name of file to read. */
  103.     char *value;        /* Should match contents of file. */
  104.     char *errMsg;        /* Error message if there's a mismatch. */
  105. {
  106.     char buf[BUFSIZ];
  107.     int count, id;
  108.  
  109.     id = open(name, O_RDONLY, 0);
  110.     if (id < 0) {
  111.     error(errMsg);
  112.     }
  113.     count = read(id, buf, 1000);
  114.     if ((count == -1) || (count == 1000)) {
  115.     error(errMsg);
  116.     }
  117.     buf[count] = 0;
  118.     if (strcmp(buf, value) != 0) {
  119.     error(errMsg);
  120.     }
  121.     close(id);
  122. }
  123.  
  124. /*
  125.  *----------------------------------------------------------------------
  126.  *
  127.  * TsCheck --
  128.  *
  129.  *    Procedure for comparing two special-purpose structures, for
  130.  *    testing fread and fwrite.
  131.  *
  132.  * Results:
  133.  *    None.
  134.  *
  135.  * Side effects:
  136.  *    Aborts if ts1 and ts2 aren't equal.
  137.  *
  138.  *----------------------------------------------------------------------
  139.  */
  140.  
  141. typedef struct {
  142.     int x;
  143.     char c[6];
  144.     short y;
  145. } testStruct;
  146.  
  147. void
  148. TsCheck(ts1, ts2, msg)
  149.     testStruct *ts1, *ts2;        /* Two structures to compare for
  150.                      * equality. */
  151.     char *msg;                /* Message to print if mismatch. */
  152. {
  153.     int i;
  154.     if ((ts1->x != ts2->x) || (ts1->y != ts2->y)) {
  155.     error(msg);
  156.     }
  157.     for (i = 0; i < 6; i++) {
  158.     if (ts1->c[i] != ts2->c[i]) {
  159.         error(msg);
  160.     }
  161.     }
  162. }
  163.  
  164. /*
  165.  *----------------------------------------------------------------------
  166.  *
  167.  * CheckNext --
  168.  *
  169.  *    Procedure for testing fprintf:  reads back from a file and
  170.  *    makes sure what was printed had the right length and content..
  171.  *
  172.  * Results:
  173.  *    None.
  174.  *
  175.  * Side effects:
  176.  *    Aborts if the contents of the file's next line don't match string
  177.  *    and length.
  178.  *
  179.  *----------------------------------------------------------------------
  180.  */
  181.  
  182. void
  183. CheckNext(f, string, length, msg)
  184.     FILE *f;            /* File from which to read a line. */
  185.     char *string;        /* This should match exactly the line. */
  186.     int length;            /* The line should be this long. */
  187.     char *msg;            /* Error message to print if there's
  188.                  * a problem. */
  189. {
  190.     char buffer[500];
  191.     if (fgets(buffer, 500, f) == NULL) {
  192.     error(msg);
  193.     }
  194.     if (strlen(buffer) != length) {
  195.     error(msg);
  196.     }
  197.     if (strcmp(buffer, string) != 0) {
  198.     error(msg);
  199.     }
  200. }
  201.  
  202. /*
  203.  *----------------------------------------------------------------------
  204.  *
  205.  * CheckFp --
  206.  *
  207.  *    This procedure is used to check floating-point scanning.  It
  208.  *    prints the numbers, so as to do an approximate check, rather than
  209.  *    an exact one (fp conversion isn't exact, after all)..
  210.  *
  211.  * Results:
  212.  *    None.
  213.  *
  214.  * Side effects:
  215.  *    Aborts if the printed version of f1-f4 don't match string.
  216.  *
  217.  *----------------------------------------------------------------------
  218.  */
  219.  
  220. void
  221. CheckFp(count1, count2, f1, f2, f3, f4, string, msg)
  222.     int count1, count2;        /* These two counts better match. */
  223.     double f1, f2, f3, f4;    /* Four numbers; when printed in %e format,
  224.                  * they better match string. */
  225.     char *string;        /* Desired values of numbers. */
  226.     char *msg;            /* Error message. */
  227. {
  228.     char printed[200];
  229.  
  230.     if (count1 != count2) {
  231.     error(msg);
  232.     }
  233.     sprintf(printed, "%e %e %e %e", f1, f2, f3, f4);
  234.     if (strcmp(printed, string) != 0) {
  235.     printf("CheckFP: %s vs. %s\n", printed, string);
  236.     error(msg);
  237.     }
  238. }
  239.  
  240. /*
  241.  *----------------------------------------------------------------------
  242.  *
  243.  * CheckString --
  244.  *
  245.  *    Utility procedure for checking sscanf on strings.  Scans input
  246.  *    according to format, into four result strings, and compares them
  247.  *    against s1-s4.
  248.  *
  249.  * Results:
  250.  *    None.
  251.  *
  252.  * Side effects:
  253.  *    Aborts if there's a mismatch.
  254.  *
  255.  *----------------------------------------------------------------------
  256.  */
  257.  
  258.  
  259. void
  260. CheckString(input, format, count, s1, s2, s3, s4, msg)
  261.     char *format;        /* Format to use for scanning. */
  262.     char *input;        /* Input string to scan. */
  263.     int count;            /* Expected return value from sscanf. */
  264.     char *s1, *s2, *s3, *s4;    /* Scanned strings should match this. */
  265.     char *msg;            /* Message to print on error. */
  266. {
  267.     char c1[100], c2[100], c3[100], c4[100];
  268.     int result, i;
  269.  
  270.     for (i = 0; i < 100; i++) {
  271.     char c;
  272.     if (i < 5) {
  273.         c = 'X';
  274.     } else {
  275.         c = 0;
  276.     }
  277.     c1[i] = c2[i] = c3[i] = c4[i] = c;
  278.     }
  279.     result = sscanf(input, format, c1, c2, c3, c4);
  280.     if ((result != count) || (strcmp(c1, s1) != 0) || (strcmp(c2, s2) != 0)
  281.         || (strcmp(c3, s3) != 0) || (strcmp(c4, s4) != 0)) {
  282.     error(msg);
  283.     }
  284. }
  285.  
  286. /*
  287.  *----------------------------------------------------------------------
  288.  *
  289.  * CheckVprintf --
  290.  *
  291.  *    This procedure is used to check vprintf:  it just packages its
  292.  *    arguments and calls vprintf.
  293.  *
  294.  * Results:
  295.  *    Whatever vprintf returns.
  296.  *
  297.  * Side effects:
  298.  *    None.
  299.  *
  300.  *----------------------------------------------------------------------
  301.  */
  302.  
  303. int
  304. CheckVprintf(va_alist)
  305.     va_dcl        /* Format string followed by one or more arguments
  306.              * to printf. */
  307. {
  308.     char *format;
  309.     va_list args;
  310.  
  311.     va_start(args);
  312.     format = va_arg(args, char *);
  313.     return vprintf(format, args);
  314. }
  315.  
  316. /*
  317.  *----------------------------------------------------------------------
  318.  *
  319.  * CheckVsprintf --
  320.  *
  321.  *    This procedure is used to check vsprintf:  it just packages its
  322.  *    arguments and calls vsprintf.
  323.  *
  324.  * Results:
  325.  *    Whatever vsprintf returns.
  326.  *
  327.  * Side effects:
  328.  *    None.
  329.  *
  330.  *----------------------------------------------------------------------
  331.  */
  332.  
  333. char *
  334. CheckVsprintf(va_alist)
  335.     va_dcl        /* Format string followed by one or more arguments
  336.              * to printf. */
  337. {
  338.     char *string;
  339.     char *format;
  340.     va_list args;
  341.  
  342.     va_start(args);
  343.     string = va_arg(args, char *);
  344.     format = va_arg(args, char *);
  345.     return vsprintf(string, format, args);
  346. }
  347.  
  348. /*
  349.  *----------------------------------------------------------------------
  350.  *
  351.  * main --
  352.  *
  353.  *    Main program for stdio testing.
  354.  *
  355.  * Results:
  356.  *    None.
  357.  *
  358.  * Side effects:
  359.  *    Runs a whole bunch of little tests on the stdio procedures,
  360.  *    and aborts with an error message if any unexpected behavior
  361.  *    is observed.  Exits with 0 status and no output if all goes
  362.  *    well.
  363.  *
  364.  *----------------------------------------------------------------------
  365.  */
  366.  
  367. main()
  368. {
  369.     FILE *f1, *f2, *f3;
  370.     char buf[BUFSIZ], c;
  371.     int i, fd, fd2, pid, length, count;
  372.     testStruct tsArray1[20], tsArray2[20]; 
  373.     union wait status;
  374.     int d1, d2, d3, d4;
  375.     float fp1, fp2, fp3, fp4;
  376.     double dbl1, dbl2, dbl3, dbl4;
  377.     short s1, s2, s3, sa1[4], sa2[4];
  378.  
  379.     /*
  380.      * putc, fputc, fputs
  381.      */
  382.  
  383.     f1 = fopen("test", "w");
  384.     if (f1 == NULL) {
  385.     error("putc error 1\n");
  386.     }
  387.     if (putc('a', f1) == EOF) {
  388.     error("putc error 2\n");
  389.     }
  390.     if (putc('b', f1) == EOF) {
  391.     error("putc error 3\n");
  392.     }
  393.     if (fputc('c', f1) != 'c') {
  394.     error("putc error 4\n");
  395.     }
  396.     if (fputc('\n', f1) != '\n') {
  397.     error("putc error 5\n");
  398.     }
  399.     if (fputs("This is the second line.\n", f1) == EOF) {
  400.     error("putc error 6\n");
  401.     }
  402.     i = fclose(f1);
  403.     if (i != 0) {
  404.     error("putc error 7\n");
  405.     }
  406.     CheckFile("test", "abc\nThis is the second line.\n", "putc error 8\n");
  407.     f1 = fopen("test2", "w");
  408.     if (fputc(0377, f1) != 0377) {
  409.     error("putc error 9\n");
  410.     }
  411.     if (putc(0377, f1) != 0377) {
  412.     error("putc error 10\n");
  413.     }
  414.     if (fputc(-1, f1) != 0377) {
  415.     error("putc error 11\n");
  416.     }
  417.     if (putc(-1, f1) != 0377) {
  418.     error("putc error 12\n");
  419.     }
  420.     fclose(f1);
  421.     f1 = fopen("test", "r");
  422.     if (putc('a', f1) != EOF) {
  423.     error("putc error 13\n");
  424.     }
  425.     if (fputc('a', f1) != EOF) {
  426.     error("putc error 14\n");
  427.     }
  428.     if (fputs("abcde", f1) != EOF) {
  429.     error("putc error 15\n");
  430.     }
  431.  
  432.     /*
  433.      * getc, fgetc, fgets
  434.      */
  435.  
  436.     c = getc(f1);
  437.     if (c != 'a') {
  438.     error("getc error 1\n");
  439.     }
  440.     buf[4] = 'Z';
  441.     if (fgets(buf, 100, f1) == NULL) {
  442.     error("getc error 2\n");
  443.     }
  444.     if (strcmp(buf, "bc\n") != 0) {
  445.     error("getc error 3\n");
  446.     }
  447.     if (buf[4] != 'Z') {
  448.     error("getc error 4\n");
  449.     }
  450.     c = fgetc(f1);
  451.     if (c != 'T') {
  452.     error("getc error 5\n");
  453.     }
  454.     buf[10] = 'X';
  455.     if (fgets(buf, 10, f1) == NULL) {
  456.     error("getc error 6\n");
  457.     }
  458.     if (strcmp(buf, "his is th") != 0) {
  459.     error("getc error 7\n");
  460.     }
  461.     if (buf[10] != 'X') {
  462.     error("getc error 8\n");
  463.     }
  464.     buf[16] = 0377;
  465.     if (fgets(buf, 100, f1) == NULL) {
  466.     error("getc error 9\n");
  467.     }
  468.     if (strcmp(buf, "e second line.\n") != 0) {
  469.     error("getc error 8\n");
  470.     }
  471.     if (buf[16] != -1) {
  472.     error("getc error 9\n");
  473.     }
  474.     if (getc(f1) != EOF) {
  475.     error("getc error 10\n");
  476.     }
  477.     if (fgetc(f1) != EOF) {
  478.     error("getc error 11\n");
  479.     }
  480.     if (fgets(buf, 1000, f1) != NULL) {
  481.     error("getc error 12\n");
  482.     }
  483.     i = fclose(f1);
  484.     if (i != 0) {
  485.     error("getc error 13\n");
  486.     }
  487.     f1 = fopen("test", "w");
  488.     if (f1 == NULL) {
  489.     error("getc error 14\n");
  490.     }
  491.     if (getc(f1) != EOF) {
  492.     error("getc error 15\n");
  493.     }
  494.     fputs("Line with no return", f1);
  495.     fclose(f1);
  496.     f1 = fopen("test", "r");
  497.     if (f1 == NULL) {
  498.     error("getc error 16\n");
  499.     }
  500.     if (fgets(buf, 1000, f1) != NULL) {
  501.     error("getc error 17\n");
  502.     }
  503.     fclose(f1);
  504.  
  505.     /*
  506.      * putw, getw
  507.      */
  508.  
  509.     f1 = fopen("test", "w");
  510.     if (f1 == NULL) {
  511.     error("putw error 1\n");
  512.     }
  513.     putc('a', f1);
  514.     if (putw(0x010203ff, f1) != 0) {
  515.     error("putw error 2\n");
  516.     }
  517.     if (putw(0x04050607, f1) != 0) {
  518.     error("putw error 3\n");
  519.     }
  520.     if (getw(f1) != EOF) {
  521.     error("putw error 4\n");
  522.     }
  523.     fclose(f1);
  524. #ifndef LITTLEENDIAN
  525.     CheckFile("test", "a\1\2\3\377\4\5\6\7", "putw error 5(B)\n");
  526. #endif
  527. #ifndef BIGENDIAN
  528.     CheckFile("test", "a\377\3\2\1\7\6\5\4", "putw error 5(L)\n");
  529. #endif
  530.     f1 = fopen("test", "r");
  531.     if (f1 == NULL) {
  532.     error("putw error 6\n");
  533.     }
  534.     (void) getc(f1);
  535.     d1 = getw(f1);
  536.     if (feof(f1)) {
  537.     error("putw error 7\n");
  538.     }
  539.     d2 = getw(f1);
  540.     if (feof(f1)) {
  541.     error("putw error 8\n");
  542.     }
  543.     d3 = getw(f1);
  544.     if (!feof(f1)) {
  545.     error("putw error 9\n");
  546.     }
  547.     if ((d1 != 0x010203ff) || (d2 != 0x04050607) || (d3 != EOF)) {
  548.     error("putw error 10\n");
  549.     }
  550.     if (putw(1, f1) != EOF) {
  551.     error("putw error 11\n");
  552.     }
  553.     fclose(f1);
  554.  
  555.     /*
  556.      * fileno
  557.      */
  558.  
  559.     if (fileno(stdin) != 0) {
  560.     error("fileno error 1\n");
  561.     }
  562.     if (fileno(stdout) != 1) {
  563.     error("fileno error 2\n");
  564.     }
  565.     if (fileno(stderr) != 2) {
  566.     error("fileno error 3\n");
  567.     }
  568.     fd = dup(1);
  569.     fclose(stdout);
  570.     f1 = fopen("test", "w");
  571.     if (fileno(f1) != 1) {
  572.     error("fileno error 4\n");
  573.     }
  574.  
  575.     /*
  576.      * putchar, puts
  577.      */
  578.  
  579.     if (putchar('x') == EOF) {
  580.     error("putchar error 1\n");
  581.     }
  582.     if (puts(" more on this line.") == EOF) {
  583.     error("putchar error 2\n");
  584.     }
  585.     if (puts("A second line.") == EOF) {
  586.     error("putchar error 3\n");
  587.     }
  588.     fclose(f1);
  589.     if (putchar('x') != EOF) {
  590.     error("putchar error 4\n");
  591.     }
  592.     if (puts("More junk.") != EOF) {
  593.     error("putchar error 5\n");
  594.     }
  595.     dup2(fd, 1);
  596.     (void) fdopen(1, "w");
  597.     CheckFile("test", "x more on this line.\nA second line.\n",
  598.         "putchar error 6\n");
  599.     close(fd);
  600.  
  601.     /*
  602.      * getchar, gets
  603.      */
  604.  
  605.     fd = dup(0);
  606.     fclose(stdin);
  607.     f1 = fopen("test", "r");
  608.     if (fileno(f1) != 0) {
  609.     error("getchar error 1\n");
  610.     }
  611.     if (getchar() != 'x') {
  612.     error("getchar error 2\n");
  613.     }
  614.     if (getchar() != ' ') {
  615.     error("getchar error 3\n");
  616.     }
  617.     if (gets(buf) != buf) {
  618.     error("gets error 1\n");
  619.     }
  620.     if (strcmp(buf, "more on this line.") != 0) {
  621.     error("gets error 2\n");
  622.     }
  623.     if (gets(buf) != buf) {
  624.     error("gets error 3\n");
  625.     }
  626.     if (strcmp(buf, "A second line.") != 0) {
  627.     error("gets error 4\n");
  628.     }
  629.     if (gets(buf) != NULL) {
  630.     error("gets error 5\n");
  631.     }
  632.     if (getchar() != EOF) {
  633.     error("getchar error 4\n");
  634.     }
  635.     dup2(fd, 0);
  636.     (void) fdopen(0, "r");
  637.     close(fd);
  638.  
  639.     /*
  640.      * fwrite, fread
  641.      */
  642.  
  643.     for (i = 0; i < 10; i++) {
  644.     int j;
  645.  
  646.     tsArray1[i].x = 0x52ff6984 + i;
  647.     tsArray1[i].y = 47+i;
  648.     for (j = 0; j < 6; j++) {
  649.         tsArray1[i].c[j] = 'A' + 2*i + j;
  650.     }
  651.     }
  652.     f1 = fopen("test", "w");
  653.     f2 = fopen("test", "r");
  654.     if (f1 == NULL) {
  655.     error("fwrite error 1\n");
  656.     }
  657.     if (f2 == NULL) {
  658.     error("fread error 1\n");
  659.     }
  660.     if (fwrite(tsArray1, sizeof(testStruct), 10, f1) != 10) {
  661.     error("fwrite error 2\n");
  662.     }
  663.     if (fwrite(&tsArray1[3], sizeof(testStruct), 5, f1) != 5) {
  664.     error("fwrite error 3\n");
  665.     }
  666.     if (fwrite(tsArray1, sizeof(testStruct), 1, f1) != 1) {
  667.     error("fwrite error 4\n");
  668.     }
  669.     if (fwrite(tsArray1, sizeof(testStruct), 1, f2) != 0) {
  670.     error("fwrite error 5\n");
  671.     }
  672.     fclose(f1);
  673.     if (fread(tsArray2, sizeof(testStruct), 2, f2) != 2) {
  674.     error("fread error 2\n");
  675.     }
  676.     TsCheck(&tsArray1[0], &tsArray2[0], "fread error 3\n");
  677.     TsCheck(&tsArray1[1], &tsArray2[1], "fread error 4\n");
  678.     if (fread(tsArray2, sizeof(testStruct), 10, f2) != 10) {
  679.     error("fread error 5\n");
  680.     }
  681.     for (i = 0; i < 10; i++) {
  682.     if (i >= 8) {
  683.         TsCheck(&tsArray1[i-5], &tsArray2[i], "fread error 6\n");
  684.     } else {
  685.         TsCheck(&tsArray1[i+2], &tsArray2[i], "fread error 7\n");
  686.     }
  687.     }
  688.     if (fread(tsArray2, sizeof(testStruct), 1, f2) != 1) {
  689.     error("fread error 8\n");
  690.     }
  691.     TsCheck(&tsArray1[5], &tsArray2[0], "fread error 9\n");
  692.     if (fread(tsArray2, sizeof(testStruct), 5, f2) != 3) {
  693.     error("fread error 10\n");
  694.     }
  695.     TsCheck(&tsArray1[6], &tsArray2[0], "fread error 11\n");
  696.     TsCheck(&tsArray1[7], &tsArray2[1], "fread error 12\n");
  697.     TsCheck(&tsArray1[0], &tsArray2[2], "fread error 13\n");
  698.     if (fread(tsArray2, sizeof(testStruct), 1, f2) != 0) {
  699.     error("fread error 14\n");
  700.     }
  701.     fclose(f2);
  702.  
  703.     /*
  704.      * fseek, rewind, and ftell
  705.      */
  706.  
  707.     f1 = fopen("test", "w+");
  708.     if (f1 == NULL) {
  709.     error("fseek error 1\n");
  710.     }
  711.     fputs("abcdefghijklmnopqrstuvwxyz", f1);
  712.     if (ftell(f1) != 26) {
  713.     error("fseek error 2\n");
  714.     }
  715.     fseek(f1, 2, SEEK_SET);
  716.     putc('1', f1);
  717.     if (ftell(f1) != 3) {
  718.     error("fseek error 3\n");
  719.     }
  720.     fseek(f1, 4, SEEK_CUR);
  721.     putc('2', f1);
  722.     if (ftell(f1) != 8) {
  723.     error("fseek error 4\n");
  724.     }
  725.     fseek(f1, -1, SEEK_END);
  726.     fputs("345", f1);
  727.     if (ftell(f1) != 28) {
  728.     error("fseek error 5\n");
  729.     }
  730.     fflush(f1);
  731.     CheckFile("test", "ab1defg2ijklmnopqrstuvwxy345", "fseek error 6\n");
  732.     fseek(f1, 1, SEEK_SET);
  733.     if (getc(f1) != 'b') {
  734.     error("fseek error 7\n");
  735.     }
  736.     if (ftell(f1) != 2) {
  737.     error("fseek error 8\n");
  738.     }
  739.     fseek(f1, -2, SEEK_END);
  740.     if (ftell(f1) != 26) {
  741.     error("fseek error 9\n");
  742.     }
  743.     if (getc(f1) != '4') {
  744.     error("fseek error 10\n");
  745.     }
  746.     if (getc(f1) != '5') {
  747.     error("fseek error 11\n");
  748.     }
  749.     if (getc(f1) != EOF) {
  750.     error("fseek error 12\n");
  751.     }
  752.     if (fseek(f1, -1, SEEK_CUR) != 0) {
  753.     error("fseek error 13\n");
  754.     }
  755.     if (getc(f1) != '5') {
  756.     error("fseek error 14\n");
  757.     }
  758.     rewind(f1);
  759.     if (getc(f1) != 'a') {
  760.     error("fseek error 15\n");
  761.     }
  762.     fclose(f1);
  763.     /*
  764.      * Check fseeks across buffer boundaries and with both read-only
  765.      * streams and read-write streams.
  766.      */
  767.     f1 = fopen("test", "w");
  768.     if (f1 == NULL) {
  769.     error("fseek error 16\n");
  770.     }
  771.     for (i = 0; i <= 3 * BUFSIZ - 1; i++) {
  772.     putc(i&0xff, f1);
  773.     }
  774.     fclose(f1);
  775.     f1 = fopen("test", "r");
  776.     if (f1 == NULL) {
  777.     error("fseek error 16a\n");
  778.     }
  779.     if (getc(f1) != 0) {
  780.     error("fseek error 17\n");
  781.     }
  782.     fseek(f1, 0, SEEK_SET);
  783.     if (getc(f1) != 0) {
  784.     error("fseek error 17a\n");
  785.     }
  786.     fseek(f1, BUFSIZ-1, SEEK_SET);
  787.     if (getc(f1) != ((BUFSIZ-1) & 0xff)) {
  788.     error("fseek error 18\n");
  789.     }
  790.     fseek(f1, BUFSIZ+1, SEEK_SET);
  791.     if (getc(f1) != ((BUFSIZ+1) & 0xff)) {
  792.     error("fseek error 19\n");
  793.     }
  794.     if (getc(f1) != ((BUFSIZ+2) & 0xff)) {
  795.     error("fseek error 20\n");
  796.     }
  797.     fseek(f1, 3 * BUFSIZ - 2, SEEK_SET);
  798.     if (getc(f1) != ((3 * BUFSIZ - 2) & 0xff)) {
  799.     error("fseek error 21\n");
  800.     }
  801.     if (getc(f1) != ((3 * BUFSIZ - 1) & 0xff)) {
  802.     error("fseek error 22\n");
  803.     }
  804.     if (getc(f1) != EOF) {
  805.     error("fseek error 23\n");
  806.     }
  807.     fclose(f1);
  808.     f1 = fopen("test", "r+");
  809.     if (f1 == NULL) {
  810.     error("fseek error 24\n");
  811.     }
  812.     for (i = 0; i < 3 * BUFSIZ - 1; i++) {
  813.     putc(i&0xff, f1);
  814.     }
  815.     fseek(f1, 0, SEEK_SET);
  816.     if (getc(f1) != 0) {
  817.     error("fseek error 25\n");
  818.     }
  819.     fseek(f1, BUFSIZ-1, SEEK_SET);
  820.     if (getc(f1) != ((BUFSIZ-1) & 0xff)) {
  821.     error("fseek error 26\n");
  822.     }
  823.     fseek(f1, BUFSIZ+1, SEEK_SET);
  824.     if (getc(f1) != ((BUFSIZ+1) & 0xff)) {
  825.     error("fseek error 27\n");
  826.     }
  827.     if (getc(f1) != ((BUFSIZ+2) & 0xff)) {
  828.     error("fseek error 28\n");
  829.     }
  830.     fseek(f1, 3 * BUFSIZ - 2, SEEK_SET);
  831.     if (getc(f1) != ((3 * BUFSIZ - 2) & 0xff)) {
  832.     error("fseek error 29\n");
  833.     }
  834.     if (getc(f1) != ((3 * BUFSIZ - 1) & 0xff)) {
  835.     error("fseek error 30\n");
  836.     }
  837.     if (getc(f1) != EOF) {
  838.     error("fseek error 31\n");
  839.     }
  840.     fclose(f1);
  841.     
  842.     
  843.  
  844.     /*
  845.      * Buffering: setbuf, setbuffer, setlinebuf, setvbuf, fflush
  846.      */
  847.  
  848.     f1 = fopen("test", "w");
  849.     if (f1 == NULL) {
  850.     error("setvbuf error 1\n");
  851.     }
  852.     if (setvbuf(f1, buf, _IOFBF, 10) != 0) {
  853.     error("setvbuf error 2\n");
  854.     }
  855.     fputs("abcd\nefgh", f1);
  856.     if (strncmp(buf, "abcd\nefgh", 9) != 0) {
  857.     error("setvbuf error 3\n");
  858.     }
  859.     CheckFile("test", "", "setvbuf error 4\n");
  860.     putc('i', f1);
  861.     CheckFile("test", "abcd\nefghi", "setvbuf error 5\n");
  862.     fseek(f1, 0, SEEK_SET);
  863.     if (setvbuf(f1, 0, _IOLBF, 100) != 0) {
  864.     error("setvbuf error 6\n");
  865.     }
  866.     buf[0] = buf[1] = 0;
  867.     fputs("123456\n7890", f1);
  868.     if (buf[0] != 0) {
  869.     error("setvbuf error 7\n");
  870.     }
  871.     CheckFile("test", "123456\nghi", "setvbuf error 8\n");
  872.     if (fflush(f1) != 0) {
  873.     error("setvbuf error 9\n");
  874.     }
  875.     CheckFile("test", "123456\n7890", "setvbuf error 10\n");
  876.     putc('X', f1);
  877.     CheckFile("test", "123456\n7890", "setvbuf error 11\n");
  878.     if (setvbuf(f1, 0, _IONBF, BUFSIZ) != 0) {
  879.     error("setvbuf error 11\n");
  880.     }
  881.     CheckFile("test", "123456\n7890X", "setvbuf error 12\n");
  882.     putc('Y', f1);
  883.     CheckFile("test", "123456\n7890XY", "setvbuf error 13\n");
  884.     if (setvbuf(f1, 0, _IOFBF, 1) != 0) {
  885.     error("setvbuf error 14\n");
  886.     }
  887.     putc('Z', f1);
  888.     CheckFile("test", "123456\n7890XYZ", "setvbuf error 15\n");
  889.     setbuf(f1, buf);
  890.     for (i = 0; i < BUFSIZ-1; i++) {
  891.     putc(i&0xff, f1);
  892.     }
  893.     if ((buf[0] != 0) || ((buf[BUFSIZ-2] & 0xff) != ((BUFSIZ-2)&0xff))) {
  894.     error("setbuf error 1\n");
  895.     }
  896.     CheckFile("test", "123456\n7890XYZ", "setbuf error 2\n");
  897.     f2 = fopen("test", "r");
  898.     fseek(f2, 14, SEEK_SET);
  899.     if (getc(f2) != EOF) {
  900.     error("setbuf error 2\n");
  901.     }
  902.     putc('a', f1);
  903.     clearerr(f2);
  904.     c = getc(f2);
  905.     if (c != 0) {
  906.     error("setbuf error 3\n");
  907.     }
  908.     fseek(f2, BUFSIZ-3, SEEK_CUR);
  909.     c = getc(f2);
  910.     if ((c&0xff) != ((BUFSIZ-2)&0xff)) {
  911.     error("setbuf error 4\n");
  912.     }
  913.     if (getc(f2) != 'a') {
  914.     error("setbuf error 5\n");
  915.     }
  916.     if (getc(f2) != EOF) {
  917.     error("setbuf error 6\n");
  918.     }
  919.     fseek(f1, 0, SEEK_SET);
  920.     clearerr(f2);
  921.     fseek(f2, 0, SEEK_SET);
  922.     setlinebuf(f1);
  923.     putc('a', f1);
  924.     c = getc(f2);
  925.     if (c != '1') {
  926.     error("setlinebuf error 1\n");
  927.     }
  928.     putc('\n', f1);
  929.     f2 = freopen("test", "r", f2);
  930.     if (getc(f2) != 'a') {
  931.     error("setlinebuf error 2\n");
  932.     }
  933.     if (getc(f2) != '\n') {
  934.     error("setlinebuf error 3\n");
  935.     }
  936.     fclose(f2);
  937.     fclose(f1);
  938.     f1 = fopen("test", "w");
  939.     if (f1 == NULL) {
  940.     error("setbuffer error 1\n");
  941.     }
  942.     setbuffer(f1, buf, 5);
  943.     fputs("ABCD", f1);
  944.     if (strncmp(buf, "ABCD", 4) != 0) {
  945.     error("setbuffer error 2\n");
  946.     }
  947.     CheckFile("test", "", "setbuffer error 3\n");
  948.     putc('E', f1);
  949.     CheckFile("test", "ABCDE", "setbuffer error 4\n");
  950.     setbuffer(f1, 0, 1000);
  951.     putc('F', f1);
  952.     CheckFile("test", "ABCDEF", "setbuffer error 5\n");
  953.     fclose(f1);
  954.     fclose(f2);
  955.  
  956.     /*
  957.      * ungetc
  958.      */
  959.  
  960.     f1 = fopen("test", "r");
  961.     (void) getc(f1);
  962.     (void) getc(f1);
  963.     if (ungetc('x', f1) != 'x') {
  964.     error("ungetc error 1\n");
  965.     }
  966.     if (ungetc('y', f1) != 'y') {
  967.     error("ungetc error 2\n");
  968.     }
  969.     if (ungetc('a', f1) != EOF) {
  970.     error("ungetc error 3\n");
  971.     }
  972.     fgets(buf, 100, f1);
  973.     if (getc(f1) != EOF) {
  974.     error("ungetc error 4\n");
  975.     }
  976.     if (ungetc('#', f1) != '#') {
  977.     error("ungetc error 5\n");
  978.     }
  979.     if (ungetc(EOF, f1) != EOF) {
  980.     error("ungetc error 6\n");
  981.     }
  982.     if (getc(f1) != '#') {
  983.     error("ungetc error 7\n");
  984.     }
  985.     if (getc(f1) != EOF) {
  986.     error("ungetc error 8\n");
  987.     }
  988.  
  989.     /*
  990.      * fopen
  991.      */
  992.  
  993.     f1 = fopen("test", "w");
  994.     if (f1 == NULL) {
  995.     error("fopen error 1\n");
  996.     }
  997.     CheckFile("test", "", "fopen error 2\n");
  998.     fputs("Test output", f1);
  999.     fseek(f1, 0, SEEK_SET);
  1000.     if (getc(f1) != EOF) {
  1001.     error("fopen error 3\n");
  1002.     }
  1003.     fclose(f1);
  1004.     f1 = fopen("test", "r");
  1005.     if (f1 == NULL) {
  1006.     error("fopen error 4\n");
  1007.     }
  1008.     if (getc(f1) != 'T') {
  1009.     error("fopen error 5\n");
  1010.     }
  1011.     if (putc('x', f1) != EOF) {
  1012.     error("fopen error 6\n");
  1013.     }
  1014.     fclose(f1);
  1015.     f1 = fopen("test", "a");
  1016.     if (f1 == NULL) {
  1017.     error("fopen error 7\n");
  1018.     }
  1019.     fputs(" continued", f1);
  1020.     fseek(f1, 0, SEEK_SET);
  1021.     fputs(" again", f1);
  1022.     fclose(f1);
  1023.     CheckFile("test", " againutput continued", "fopen error 8\n");
  1024.     f1= fopen("test", "w");
  1025.     fputs("Test output continued again", f1);
  1026.     fclose(f1);
  1027.     f1= fopen("test", "rb+");
  1028.     if (f1 == NULL) {
  1029.     error("fopen error 9\n");
  1030.     }
  1031.     if (getc(f1) != 'T') {
  1032.     error("fopen error 10\n");
  1033.     }
  1034.     fseek(f1, 0, SEEK_CUR);
  1035.     if (fputs("urd", f1) == EOF) {
  1036.     error("fopen error 11\n");
  1037.     }
  1038.     fclose(f1);
  1039.     CheckFile("test", "Turd output continued again", "fopen error 12\n");
  1040.     f1 = fopen("test", "w+");
  1041.     if (f1 == NULL) {
  1042.     error("fopen error 13\n");
  1043.     }
  1044.     CheckFile("test", "", "fopen error 14\n");
  1045.     if (fputs("Again", f1) == EOF) {
  1046.     error("fopen error 15\n");
  1047.     }
  1048.     fclose(f1);
  1049.     f1 = fopen("test", "a+b");
  1050.     if (f1 == NULL) {
  1051.     error("fopen error 16\n");
  1052.     }
  1053.     fputs(" written", f1);
  1054.     fflush(f1);
  1055.     CheckFile("test", "Again written", "fopen error 17\n");
  1056.     fseek(f1, 0, SEEK_SET);
  1057.     if (getc(f1) != 'A') {
  1058.     error("fopen error 18\n");
  1059.     }
  1060.     fseek(f1, 0, SEEK_CUR);
  1061.     fputs(" to\n", f1);
  1062.     fclose(f1);
  1063.     CheckFile("test", "A to\n written", "fopen error 19\n");
  1064.     f1 = fopen("test", "w");
  1065.     fputs("Again written to\n", f1);
  1066.     fclose(f1);
  1067.     if (fopen("test", "q") != NULL) {
  1068.     error("fopen error 20\n");
  1069.     }
  1070.     if (fopen("test", "ba") != NULL) {
  1071.     error("fopen error 21\n");
  1072.     }
  1073.  
  1074.     /*
  1075.      * fdopen, freopen
  1076.      */
  1077.  
  1078.     fd = open("test", O_RDONLY, 0666);
  1079.     if (fd == -1) {
  1080.     error("fdopen error 1\n");
  1081.     }
  1082.     f1 = fdopen(fd, "r");
  1083.     if (fgets(buf, 100, f1) == NULL) {
  1084.     error("fdopen error 2\n");
  1085.     }
  1086.     if (strcmp(buf, "Again written to\n") != 0) {
  1087.     error("fdopen error 3\n");
  1088.     }
  1089.     /*
  1090.      * Tricky stuff:  it must be possible to have two FILE's open
  1091.      * simultaneously on the same stream id.  Sounds crazy, but some
  1092.      * UNIX programs depend on it.
  1093.      */
  1094.     f2 = fopen("test2", "w");
  1095.     f3 = fdopen(fileno(f2), "w");
  1096.     if (fputs("Test1", f2) == NULL) {
  1097.     error("fdopen error 4\n");
  1098.     }
  1099.     if (fputs("Test2", f3) == NULL) {
  1100.     error("fdopen error 5\n");
  1101.     }
  1102.     if (fputs(" and test3", f2) == NULL) {
  1103.     error("fdopen error 6\n");
  1104.     }
  1105.     if (fputs(" and test4", f3) == NULL) {
  1106.     error("fdopen error 7\n");
  1107.     }
  1108.     fflush(f2);
  1109.     fflush(f3);
  1110.     CheckFile("test2", "Test1 and test3Test2 and test4", "fdopen error 8\n");
  1111.     fclose(f2);
  1112.     fclose(f3);
  1113.     /*
  1114.      * More tricky stuff:  fdopen must seek to the end of the file in
  1115.      * "a" mode:  some UNIX programs depend on it.
  1116.      */
  1117.     fd = open("test2", O_WRONLY, 0666);
  1118.     if (fd == -1) {
  1119.     error("fdopen error 9\n");
  1120.     }
  1121.     f2 = fdopen(fd, "a");
  1122.     fputs(" and more", f2);
  1123.     fclose(f2);
  1124.     CheckFile("test2", "Test1 and test3Test2 and test4 and more",
  1125.         "fdopen error 10\n");
  1126.  
  1127.     f1 = freopen("test2", "w", f1);
  1128.     if (f1 == NULL) {
  1129.     error("freopen error 1\n");
  1130.     }
  1131.     if (fputs("Output data\n", f1) == EOF) {
  1132.     error("freopen error 2\n");
  1133.     }
  1134.     f1 = freopen("test2", "r", f1);
  1135.     if (f1 == NULL) {
  1136.     error("freopen error 3\n");
  1137.     }
  1138.     if (fgets(buf, 100, f1) == NULL) {
  1139.     error("freopen error 4\n");
  1140.     }
  1141.     if (strcmp(buf, "Output data\n") != 0) {
  1142.     error("freopen error 5\n");
  1143.     }
  1144.     CheckFile("test2", "Output data\n", "freopen error 6\n");
  1145.     fclose(f1);
  1146.  
  1147.     /*
  1148.      * feof, ferror, clearerr
  1149.      */
  1150.  
  1151.     f1 = fopen("test", "r");
  1152.     f2 = fopen("test", "w");
  1153.     if ((f1 == NULL) || (f2 == NULL)) {
  1154.     error("ferror error 1\n");
  1155.     }
  1156.     if (getc(f1) != EOF) {
  1157.     error("ferror error 2\n");
  1158.     }
  1159.     if (!feof(f1)) {
  1160.     error("ferror error 3\n");
  1161.     }
  1162.     putc('a', f2);
  1163.     fclose(f2);
  1164.     if (getc(f1) != EOF) {
  1165.     error("ferror error 4\n");
  1166.     }
  1167.     if (!feof(f1)) {
  1168.     error("ferror error 5\n");
  1169.     }
  1170.     clearerr(f1);
  1171.     if (feof(f1)) {
  1172.     error("ferror error 6\n");
  1173.     }
  1174.     if (getc(f1) != 'a') {
  1175.     error("ferror error 7\n");
  1176.     }
  1177.     if (feof(f1)) {
  1178.     error("ferror error 8\n");
  1179.     }
  1180.     if (getc(f1) != EOF) {
  1181.     error("ferror error 9\n");
  1182.     }
  1183.     if (!feof(f1)) {
  1184.     error("ferror error 10\n");
  1185.     }
  1186.     fclose(f1);
  1187.     fd = open("test", O_RDONLY, 0666);
  1188.     if (fd == -1) {
  1189.     error("ferror error 11\n");
  1190.     }
  1191.     f1 = fdopen(fd, "r+");
  1192.     setvbuf(f1, 0, _IONBF, 1);
  1193.     if (putc('x', f1) != EOF) {
  1194.     error("ferror error 12\n");
  1195.     }
  1196.     if (ferror(f1) == 0) {
  1197.     error("ferror error 13\n");
  1198.     }
  1199.     fseek(f1, 0, 0);
  1200.     if (getc(f1) != EOF) {
  1201.     error("ferror error 14\n");
  1202.     }
  1203.     if (ferror(f1) == 0) {
  1204.     error("ferror error 15\n");
  1205.     }
  1206.     clearerr(f1);
  1207.     if (ferror(f1) != 0) {
  1208.     error("ferror error 16\n");
  1209.     }
  1210.     if (getc(f1) != 'a') {
  1211.     error("ferror error 17\n");
  1212.     }
  1213.     fclose(f1);
  1214.  
  1215.     /*
  1216.      * _cleanup
  1217.      */
  1218.  
  1219.     if (fork() == 0) {
  1220.     f1 = fopen("test", "w");
  1221.     if (f1 == NULL) {
  1222.         error("_cleanup error 1\n");
  1223.     }
  1224.     fputs("Test string", f1);
  1225.     exit(0);
  1226.     }
  1227.     wait(&status);
  1228.     if (status.w_T.w_Retcode == 0) {
  1229.     CheckFile("test", "Test string", "_cleanup error 2\n");
  1230.     }
  1231.  
  1232.     /*
  1233.      * fprintf
  1234.      */
  1235.  
  1236.     f1 = fopen("test", "w");
  1237.     f2 = fopen("test", "r");
  1238.     if ((f1 == NULL) || (f2 == NULL)) {
  1239.     error("sprintf error 1\n");
  1240.     }
  1241.     setvbuf(f1, 0, _IOLBF, BUFSIZ);
  1242.     length = fprintf(f1, "%*d %d %d %d\n", 6, 34, 16923, -12, -1);
  1243.     CheckNext(f2, "    34 16923 -12 -1\n", length, "fprintf error 2\n");
  1244.     length = fprintf(f1, "%4d %4d %4d %4d %d %#x %#X\n", 6, 34,
  1245.         16923, -12, -1, 0, 0);
  1246.  
  1247.     CheckNext(f2, "   6   34 16923  -12 -1 0 0\n", length,
  1248.         "fprintf error 3\n");
  1249.  
  1250.     length = fprintf(f1, "%4u %4u %4u %4u %d %#o\n", 6, 34, 16923, -12, -1, 0);
  1251.     CheckNext(f2, "   6   34 16923 4294967284 -1 0\n", length,
  1252.         "fprintf error 4\n");
  1253.  
  1254.     length = fprintf(f1, "%-4d %-4d %-4d %-4ld\n", 6, 34, 16923, -12, -1);
  1255.     CheckNext(f2, "6    34   16923 -12 \n", length,
  1256.         "fprintf error 5\n");
  1257.  
  1258.     length = fprintf(f1, "%04d %04d %04d %04d\n", 6, 34, 16923, -12, -1);
  1259.     CheckNext(f2, "0006 0034 16923 -012\n", length,
  1260.         "fprintf error 6\n");
  1261.  
  1262.     length = fprintf(f1, "%00*d\n", 6, 34, 16923, -12, -1);
  1263.     CheckNext(f2, "000034\n", length, "fprintf error 7\n");
  1264.  
  1265.     length = fprintf(f1, "%4x %4x %4x %4x\n", 6, 34, 16923, -12, -1);
  1266.     CheckNext(f2, "   6   22 421b fffffff4\n", length,
  1267.         "fprintf error 8\n");
  1268.  
  1269.     length = fprintf(f1, "%#x %#X %#X %#x\n", 6, 34, 16923, -12, -1);
  1270.     CheckNext(f2, "0x6 0X22 0X421B 0xfffffff4\n", length,
  1271.         "fprintf error 9\n");
  1272.  
  1273.     length = fprintf(f1, "%#20x %#20x %#20x %#20x\n", 6, 34, 16923, -12, -1);
  1274.     CheckNext(f2, "                 0x6                 0x22               0x421b           0xfffffff4\n", length, "fprintf error 10\n");
  1275.  
  1276.     length = fprintf(f1, "%-#20x %-#20x %-#20x %-#20x\n", 6, 34, 16923, -12, -1);
  1277.     CheckNext(f2, "0x6                  0x22                 0x421b               0xfffffff4          \n", length, "fprintf error 11\n");
  1278.  
  1279.     length = fprintf(f1, "%-#20o %#-20o %#-20o %#-20o\n", 6, 34, 16923, -12, -1);
  1280.     CheckNext(f2, "06                   042                  041033               037777777764        \n", length, "fprintf error 12\n");
  1281.  
  1282.     length = fprintf(f1, "%s %s %c %s\n", "abcd",
  1283.         "This is a very long test string.", 'x', "x");
  1284.     CheckNext(f2, "abcd This is a very long test string. x x\n", length,
  1285.         "fprintf error 13\n");
  1286.  
  1287.     length = fprintf(f1, "%20s %20s %20c %20s\n", "abcd",
  1288.         "This is a very long test string.", 'x', "x");
  1289.     CheckNext(f2, "                abcd This is a very long test string.                    x                    x\n", length, "fprintf error 14\n");
  1290.  
  1291.     length = fprintf(f1, "%.10s %.10s %c %.10s\n", "abcd",
  1292.         "This is a very long test string.", 'x', "x");
  1293.     CheckNext(f2, "abcd This is a  x x\n", length, "fprintf error 15\n");
  1294.  
  1295.     length = fprintf(f1, "%s %s %1 %% %c %s\n", "abcd",
  1296.         "This is a very long test string.", 'x', "x");
  1297.     CheckNext(f2, "abcd This is a very long test string.  % x x\n", length,
  1298.         "fprintf error 16\n");
  1299.  
  1300. #ifdef SPRITED_FLOAT
  1301.     length = fprintf(f1, "%e %e %e %e\n", 34.2e102, 16923.0/247,
  1302.         -.125, -16000., .000053);
  1303.     CheckNext(f2, "3.420000e+103 6.851417e+01 -1.250000e-01 -1.600000e+04\n", length, "fprintf error 17\n");
  1304.  
  1305.     length = fprintf(f1, "%20e %20e %20e %20e\n", 34.2e102, 16923.0/247,
  1306.         -.125, -16000., .000053);
  1307.     CheckNext(f2, "       3.420000e+103         6.851417e+01        -1.250000e-01        -1.600000e+04\n", length, "fprintf error 18\n");
  1308.  
  1309.     length = fprintf(f1, "%.1e %.1e %.1e %.1e\n", 34.2e102, 16923.0/247,
  1310.         -.125, -16000., .000053);
  1311.     CheckNext(f2, "3.4e+103 6.9e+01 -1.3e-01 -1.6e+04\n", length,
  1312.         "fprintf error 19\n");
  1313.  
  1314.     length = fprintf(f1, "%020e %020e %020e %020e\n", 34.2e102, 16923.0/247,
  1315.         -.125, -16000., .000053);
  1316.     CheckNext(f2, "00000003.420000e+103 000000006.851417e+01 -00000001.250000e-01 -00000001.600000e+04\n", length, "fprintf error 20\n");
  1317.  
  1318.     length = fprintf(f1, "%7.1e %7.1e %7.1e %7.1e\n", 34.2e102, 16923.0/247,
  1319.         -.125, -16000., .000053);
  1320.     CheckNext(f2, "3.4e+103 6.9e+01 -1.3e-01 -1.6e+04\n", length,
  1321.         "fprintf error 21\n");
  1322.  
  1323.     length = fprintf(f1, "%f %f %f %f\n", 34.2e102, 16923.0/247,
  1324.         -.125, -16000., .000053);
  1325.     CheckNext(f2, "34200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 68.514170 -0.125000 -16000.000000\n", length, "fprintf error 22\n");
  1326.  
  1327.     length = fprintf(f1, "%.4f %.4f %.4f %.4f %.4f\n", 34.2e102, 16923.0/247,
  1328.         -.125, -16000., .000053);
  1329.     CheckNext(f2, "34200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 68.5142 -0.1250 -16000.0000 0.0001\n", length, "fprintf error 23\n");
  1330. #endif /* SPRITED_FLOAT */
  1331.  
  1332.     length = fprintf(f1, "%.4e %.5e %.6e\n", -9.99995, -9.99995, 9.99995);
  1333.     CheckNext(f2, "-1.0000e+01 -9.99995e+00 9.999950e+00\n", length,
  1334.         "fprintf error 24\n");
  1335.  
  1336.     length = fprintf(f1, "%.4f %.5f %.6f\n", -9.99995, -9.99995, 9.99995);
  1337.     CheckNext(f2, "-10.0000 -9.99995 9.999950\n", length,
  1338.         "fprintf error 25\n");
  1339.  
  1340.     length = fprintf(f1, "%20f %-20f %020f\n", -9.99995, -9.99995, 9.99995);
  1341.     CheckNext(f2, "           -9.999950 -9.999950            0000000000009.999950\n", length, "fprintf error 26\n");
  1342.  
  1343.     length = fprintf(f1, "%-020f %020f\n", -9.99995, -9.99995, 9.99995);
  1344.     CheckNext(f2, "-9.999950            -000000000009.999950\n", length,
  1345.         "fprintf error 27\n");
  1346.  
  1347.     length = fprintf(f1, "%.0e %#.0e\n", -9.99995, -9.99995, 9.99995);
  1348.     CheckNext(f2, "-1e+01 -1.e+01\n", length, "fprintf error 28\n");
  1349.  
  1350.     length = fprintf(f1, "%.0f %#.0f\n", -9.99995, -9.99995, 9.99995);
  1351.     CheckNext(f2, "-10 -10.\n", length, "fprintf error 29\n");
  1352.  
  1353.     length = fprintf(f1, "%.4f %.5f %.6f\n", -9.99995, -9.99995, 9.99995);
  1354.     CheckNext(f2, "-10.0000 -9.99995 9.999950\n", length,
  1355.         "fprintf error 30\n");
  1356.  
  1357.     length = fprintf(f1, "%.3g\n", 12341.0);
  1358.     CheckNext(f2, "1.23e+04\n", length, "fprintf error 31\n");
  1359.  
  1360.     length = fprintf(f1, "%.3G\n", 1234.12345);
  1361.     CheckNext(f2, "1.23E+03\n", length, "fprintf error 32\n");
  1362.  
  1363.     length = fprintf(f1, "%.3g\n", 123.412345);
  1364.     CheckNext(f2, "123\n", length, "fprintf error 33\n");
  1365.  
  1366.     length = fprintf(f1, "%.3g\n", 12.3412345);
  1367.     CheckNext(f2, "12.3\n", length, "fprintf error 34\n");
  1368.  
  1369.     length = fprintf(f1, "%.3g\n", 1.23412345);
  1370.     CheckNext(f2, "1.23\n", length, "fprintf error 35\n");
  1371.  
  1372.     length = fprintf(f1, "%.3g\n", .123412345);
  1373.     CheckNext(f2, "0.123\n", length, "fprintf error 36\n");
  1374.  
  1375.     length = fprintf(f1, "%.3g\n", .012341);
  1376.     CheckNext(f2, "0.0123\n", length, "fprintf error 37\n");
  1377.  
  1378.     length = fprintf(f1, "%.3g\n", .0012341);
  1379.     CheckNext(f2, "0.00123\n", length, "fprintf error 38\n");
  1380.  
  1381.     length = fprintf(f1, "%.3g\n", .00012341);
  1382.     CheckNext(f2, "0.000123\n", length, "fprintf error 39\n");
  1383.  
  1384.     length = fprintf(f1, "%.3g\n", .00001234);
  1385.     CheckNext(f2, "1.23e-05\n", length, "fprintf error 40\n");
  1386.  
  1387.     length = fprintf(f1, "%.4g\n", 9999.5);
  1388.     CheckNext(f2, "1e+04\n", length, "fprintf error 41\n");
  1389.  
  1390.     length = fprintf(f1, "%.4g\n", 999.95);
  1391.     CheckNext(f2, "1000\n", length, "fprintf error 42\n");
  1392.  
  1393.     length = fprintf(f1, "%.3g\n", 1.0);
  1394.     CheckNext(f2, "1\n", length, "fprintf error 43\n");
  1395.  
  1396.     length = fprintf(f1, "%.\n", 1.0);
  1397.     CheckNext(f2, "\n", length, "fprintf error 44\n");
  1398.  
  1399.     length = fprintf(f1, "%.3g\n", .1);
  1400.     CheckNext(f2, "0.1\n", length, "fprintf error 45\n");
  1401.  
  1402.     length = fprintf(f1, "%.3g\n", .01);
  1403.     CheckNext(f2, "0.01\n", length, "fprintf error 46\n");
  1404.  
  1405.     length = fprintf(f1, "%.3g\n", .001);
  1406.     CheckNext(f2, "0.001\n", length, "fprintf error 47\n");
  1407.  
  1408.     length = fprintf(f1, "%.3g\n", .0001);
  1409.     CheckNext(f2, "1e-04\n", length, "fprintf error 48\n");
  1410.  
  1411.     length = fprintf(f1, "%.3g\n", .00001);
  1412.     CheckNext(f2, "1e-05\n", length, "fprintf error 49\n");
  1413.  
  1414.     length = fprintf(f1, "%#.3g\n", 1234.0);
  1415.     CheckNext(f2, "1.23e+03\n", length, "fprintf error 50\n");
  1416.  
  1417.     length = fprintf(f1, "%#.3G\n", 9999.5);
  1418.     CheckNext(f2, "1.00E+04\n", length, "fprintf error 51\n");
  1419.  
  1420.     length = fprintf(f1, "%e %f %g\n", 0.0, 0.0, 0.0, 0.0);
  1421.     CheckNext(f2, "0.000000e+00 0.000000 0\n", length, "fprintf error 52\n");
  1422.  
  1423.     length = fprintf(f1, "%.4e %.4f %.4g\n", 0.0, 0.0, 0.0, 0.0);
  1424.     CheckNext(f2, "0.0000e+00 0.0000 0\n", length, "fprintf error 53\n");
  1425.  
  1426.     length = fprintf(f1, "%#.4e %#.4f %#.4g\n", 0.0, 0.0, 0.0, 0.0);
  1427.     CheckNext(f2, "0.0000e+00 0.0000 0.000\n", length, "fprintf error 54\n");
  1428.  
  1429.     length = fprintf(f1, "%.0e %.0f %.0g\n", 0.0, 0.0, 0.0, 0.0);
  1430.     CheckNext(f2, "0e+00 0 0\n", length, "fprintf error 55\n");
  1431.  
  1432.     length = fprintf(f1, "%#.0e %#.0f %#.0g\n", 0.0, 0.0, 0.0, 0.0);
  1433.     CheckNext(f2, "0.e+00 0. 0.\n", length, "fprintf error 56\n");
  1434.  
  1435.     length = fprintf(f1, "%3.0f %3.0f %3.0f %3.0f\n", 0.0, 0.1, 0.01, 0.001);
  1436.     CheckNext(f2, "  0   0   0   0\n", length, "fprintf error 57\n");
  1437.  
  1438.     length = fprintf(f1, "%3.0f %3.0f %3.0f %3.0f\n", 1.0, 1.1, 1.01, 1.001);
  1439.     CheckNext(f2, "  1   1   1   1\n", length, "fprintf error 58\n");
  1440.  
  1441.     length = fprintf(f1, "%3.1f %3.1f %3.1f %3.1f\n", 0.0, 0.1, 0.01, 0.001);
  1442.     CheckNext(f2, "0.0 0.1 0.0 0.0\n", length, "fprintf error 59\n");
  1443.  
  1444.     fclose(f1);
  1445.     fclose(f2);
  1446.  
  1447.     /*
  1448.      * sscanf
  1449.      */
  1450.  
  1451.     count = sscanf("-20 1476 \n33 0", "%d %d %d %d", &d1, &d2, &d3, &d4);
  1452.     if ((count != 4) || (d1 != -20) || (d2 != 1476) || (d3 != 33)
  1453.         || (d4 != 0)) {
  1454.     error("sscanf error 1\n");
  1455.     }
  1456.  
  1457.     d4 = -1;
  1458.     count = sscanf("-45 16 7890 +10", "%2d %*d %10d %d", &d1, &d2, &d3, &d4);
  1459.     if ((count != 3) || (d1 != -4) || (d2 != 16) || (d3 != 7890)
  1460.         || (d4 != -1)) {
  1461.     error("sscanf error 2\n");
  1462.     }
  1463.  
  1464.     count = sscanf("-45 16 +10 987", "%D %  %hD %d", &d1, &d2, &d3, &d4);
  1465.     if ((count != 4) || (d1 != -45) || (d2 != 16) || (d3 != 10)
  1466.         || (d4 != 987)) {
  1467.     error("sscanf error 3\n");
  1468.     }
  1469.  
  1470.     sa1[0] = sa1[1] = sa1[2] = sa2[0] = sa2[1] = sa2[2] = -1;
  1471.     count = sscanf("14 1ab 62 10", "%hd %x %O %hx", &sa1[1], &d2, &d3,
  1472.         &sa2[1]);
  1473.     if ((count != 4) || (sa1[0] != -1) || (sa1[1] != 14) || (sa1[2] != -1)
  1474.         || (d2 != 427) || (d3 != 50) || (sa2[0] != -1) || (sa2[1] != 0x10)
  1475.         || (sa2[2] != -1)) {
  1476.     error("sscanf error 4\n");
  1477.     }
  1478.  
  1479.     count = sscanf("12345670 1234567890ab cdefg", "%o     %o %x %X",
  1480.         &d1, &d2, &d3, &d4);
  1481.     if ((count != 4) || (d1 != 2739128) || (d2 != 342391) || (d3 != 561323)
  1482.         || (d4 != 52719)) {
  1483.     error("sscanf error 5\n");
  1484.     }
  1485.  
  1486.     count = sscanf("ab123-24642", "%2x %3hx %3o %2ho", &d1, &s1, &d3, &s2);
  1487.     if ((count != 4) || (d1 != 171) || (s1 != 0x123) || (d3 != -20)
  1488.         || (s2 != 0x34)) {
  1489.     error("sscanf error 6\n");
  1490.     }
  1491.  
  1492.     d2 = d3 = d4 = -1;
  1493.     count = sscanf("42 43", "%d %", &d1, &d2, &d3, &d4);
  1494.     if ((count != -1) || (d1 != 42) || (d2 != -1) || (d3 != -1)
  1495.         || (d4 != -1)) {
  1496.     error("sscanf error 7\n");
  1497.     }
  1498.  
  1499.     d2 = d3 = d4 = -1;
  1500.     count = sscanf("42", "%d", &d1, &d2, &d3, &d4);
  1501.     if ((count != 1) || (d1 != 42) || (d2 != -1) || (d3 != -1)
  1502.         || (d4 != -1)) {
  1503.     error("sscanf error 8\n");
  1504.     }
  1505.  
  1506.     d2 = d3 = d4 = -1;
  1507.     count = sscanf("1 2 3 4 5", "%q %r", &d1, &d2, &d3, &d4);
  1508.     if ((count != 2) || (d1 != 1) || (d2 != 2) || (d3 != -1)
  1509.         || (d4 != -1)) {
  1510.     error("sscanf error 9\n");
  1511.     }
  1512.  
  1513.     d3 = d4 = -1;
  1514.     count = sscanf("1234567 234 567  ", "%*3x %x %*o %4o", &d1, &d2, &d3, &d4);
  1515.     if ((count != 2) || (d1 != 17767) || (d2 != 375) || (d3 != -1)
  1516.         || (d4 != -1)) {
  1517.     error("sscanf error 10\n");
  1518.     }
  1519.  
  1520.     d3 = d4 = -1;
  1521.     count = sscanf("1234567 234 567  ", "%*3x %x %*o %4o", &d1, &d2, &d3, &d4);
  1522.     if ((count != 2) || (d1 != 17767) || (d2 != 375) || (d3 != -1)
  1523.         || (d4 != -1)) {
  1524.     error("sscanf error 11\n");
  1525.     }
  1526.  
  1527.     d1 = d2 = d3 = d4 = -1;
  1528.     count = sscanf("a    1234", "%d %d", &d1, &d2, &d3, &d4);
  1529.     if ((count != 0) || (d1 != -1) || (d2 != -1) || (d3 != -1)
  1530.         || (d4 != -1)) {
  1531.     error("sscanf error 11\n");
  1532.     }
  1533.  
  1534.     count = sscanf("12345678", "%2d %2d %2d %2d", &d1, &d2, &d3, &d4);
  1535.     if ((count != 4) || (d1 != 12) || (d2 != 34) || (d3 != 56)
  1536.         || (d4 != 78)) {
  1537.     error("sscanf error 12\n");
  1538.     }
  1539.  
  1540.     d3 = d4 = -1;
  1541.     count = sscanf("1 2 ", "%d %d %d %d", &d1, &d2, &d3, &d4);
  1542.     if ((count != 2) || (d1 != 1) || (d2 != 2) || (d3 != -1)
  1543.         || (d4 != -1)) {
  1544.     error("sscanf error 13\n");
  1545.     }
  1546.  
  1547.     d1 = d2 = d3 = d4 = -1;
  1548.     count = sscanf("  a", " a%d %d %d %d", &d1, &d2, &d3, &d4);
  1549.     if ((count != -1) || (d1 != -1) || (d2 != -1) || (d3 != -1)
  1550.         || (d4 != -1)) {
  1551.     error("sscanf error 14\n");
  1552.     }
  1553.  
  1554.     fp4 = -1.0;
  1555.     count = sscanf("2.1 -3.0e8 .99962 a", "%f%f%f%f", &fp1, &fp2, &fp3, &fp4);
  1556.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1557.         "2.100000e+00 -3.000000e+08 9.996200e-01 -1.000000e+00",
  1558.         "sscanf error 15\n");
  1559.  
  1560.     count = sscanf("-1.2345 +8.2 9", "%3e %3f %f %f", &fp1, &fp2, &fp3, &fp4);
  1561.     CheckFp(count, 4, fp1, fp2, fp3, fp4,
  1562.         "-1.000000e+00 2.340000e+02 5.000000e+00 8.200000e+00",
  1563.         "sscanf error 16\n");
  1564.  
  1565.     fp4 = -1.0;
  1566.     count = sscanf("1e00004 332E-4 3e+4", "%f %*2e %f %f", &fp1,
  1567.         &fp2, &fp3, &fp4);
  1568.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1569.         "1.000000e+04 2.000000e-04 3.000000e+04 -1.000000e+00",
  1570.         "sscanf error 17\n");
  1571.  
  1572.     fp4 = -1.0;
  1573.     count = sscanf("1. 47.6 2.e2 3.e-", "%f %*f %f %f", &fp1, &fp2,
  1574.         &fp3, &fp4);
  1575.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1576.         "1.000000e+00 2.000000e+02 3.000000e+00 -1.000000e+00",
  1577.         "sscanf error 18\n");
  1578.  
  1579. #ifdef SPRITED_FLOAT
  1580.     /* 
  1581.      * I don't know if the mismatch between the format string and the 
  1582.      * number of arguments would cause a problem.  Even if you drop fp3 and 
  1583.      * fp4 from the sscanf, this causes a crash with the "mipsfree" MK63.
  1584.      */
  1585.     fp3 = fp4 = -1.0;
  1586.     count = sscanf("1.eabc", "%f %x", &fp1, &fp2, &fp3, &fp4);
  1587.     CheckFp(count, 2, fp1, fp2, fp3, fp4,
  1588.         "1.000000e+00 3.850768e-42 -1.000000e+00 -1.000000e+00",
  1589.         "sscanf error 19\n");
  1590. #endif
  1591.  
  1592.     count = sscanf("4.6 99999.7 876.43e-1 118", "%f %f %f %e",
  1593.         &fp1, &fp2, &fp3, &fp4);
  1594.     CheckFp(count, 4, fp1, fp2, fp3, fp4,
  1595.         "4.600000e+00 9.999970e+04 8.764300e+01 1.180000e+02",
  1596.         "sscanf error 20\n");
  1597.  
  1598.     count = sscanf("1.2345 697.0e-3 124 .00005", "%F %E %lf %le",
  1599.         &dbl1, &dbl2, &dbl3, &dbl4);
  1600.     CheckFp(count, 4, dbl1, dbl2, dbl3, dbl4,
  1601.         "1.234500e+00 6.970000e-01 1.240000e+02 5.000000e-05",
  1602.         "sscanf error 21\n");
  1603.  
  1604.     count = sscanf("4.6 99999.7 876.43e-1 118", "%F %F %F %F",
  1605.         &dbl1, &dbl2, &dbl3, &dbl4);
  1606.     CheckFp(count, 4, dbl1, dbl2, dbl3, dbl4,
  1607.         "4.600000e+00 9.999970e+04 8.764300e+01 1.180000e+02",
  1608.         "sscanf error 22\n");
  1609.  
  1610.     dbl2 = dbl3 = dbl4 = -1.0;
  1611.     count = sscanf("4.6abc", "%F %F %f %f",
  1612.         &dbl1, &dbl2, &dbl3, &dbl4);
  1613.     CheckFp(count, 1, dbl1, dbl2, dbl3, dbl4,
  1614.         "4.600000e+00 -1.000000e+00 -1.000000e+00 -1.000000e+00",
  1615.         "sscanf error 23\n");
  1616.  
  1617.     dbl3 = dbl4 = -1.0;
  1618.     count = sscanf("4.6 5.2", "%F %F %F %F",
  1619.         &dbl1, &dbl2, &dbl3, &dbl4);
  1620.     CheckFp(count, 2, dbl1, dbl2, dbl3, dbl4,
  1621.         "4.600000e+00 5.200000e+00 -1.000000e+00 -1.000000e+00",
  1622.         "sscanf error 24\n");
  1623.  
  1624.     CheckString("abc defghijk dum ", "%s %3s %20s %s", 4,
  1625.         "abc", "def", "ghijk", "dum",
  1626.         "sscanf error 25\n");
  1627.  
  1628.     CheckString("abc       bcdef", "%5c%c%1s %s", 4,
  1629.         "abc  ", " XXXX", "b", "cdef",
  1630.         "sscanf error 26\n");
  1631.  
  1632.     CheckString("123456 test ", "%*c%*s %s %s %s", 1,
  1633.         "test", "XXXXX", "XXXXX", "XXXXX",
  1634.         "sscanf error 27\n");
  1635.  
  1636.     CheckString("ababcd01234  f 123450", "%4[abcd] %4[abcd] %[^abcdef] %[^0]",
  1637.         4, "abab", "cd", "01234  ", "f 12345",
  1638.         "sscanf error 28\n");
  1639.  
  1640.     CheckString("aaaaaabc aaabcdefg  + +  XYZQR",
  1641.         "%*4[a] %s %*4[a]%s%*4[ +]%10c", 3,
  1642.         "aabc", "bcdefg", "+  XYZQR", "XXXXX",
  1643.         "sscanf error 29\n");
  1644.  
  1645.     /*
  1646.      * printf, scanf, fscanf, sprintf, vprintf, vsprintf
  1647.      */
  1648.  
  1649.     fd = dup(1);
  1650.     fclose(stdout);
  1651.     f1 = fopen("test", "w");
  1652.     count = printf("%d and %d", 47, 102);
  1653.     if (count != 10) {
  1654.     error("printf error 1\n");
  1655.     }
  1656.     count = CheckVprintf("  %x then %x", 15, 65);
  1657.     if (count != 11) {
  1658.     error("printf error 2\n");
  1659.     }
  1660.     fclose(stdout);
  1661.     CheckFile("test", "47 and 102  f then 41", "printf error 3\n");
  1662.     dup2(fd, 1);
  1663.     (void) fdopen(1, "w");
  1664.     close(fd);
  1665.  
  1666.     fd = dup(0);
  1667.     fclose(stdin);
  1668.     f1 = fopen("test", "r");
  1669.     count = scanf("%d and %d %d", &d1, &d2, &d3);
  1670.     if ((count != 2) || (d1 != 47) || (d2 != 102)) {
  1671.     error("scanf error 1\n");
  1672.     }
  1673.     fclose(stdin);
  1674.     dup2(fd, 0);
  1675.     (void) fdopen(0, "r");
  1676.     close(fd);
  1677.  
  1678.     f1 = fopen("test", "r");
  1679.     count = fscanf(f1, "%d and %d %d", &d1, &d2, &d3);
  1680.     if ((count != 2) || (d1 != 47) || (d2 != 102)) {
  1681.     error("fscanf error 1\n");
  1682.     }
  1683.     fclose(f1);
  1684.  
  1685.     if (sprintf(buf, "%s %d", "The value is", 19) != buf) {
  1686.     error("sprintf error 1\n");
  1687.     }
  1688.     if (strcmp(buf, "The value is 19") != 0) {
  1689.     error("sprintf error 2\n");
  1690.     }
  1691.  
  1692.     if (CheckVsprintf(buf, "%s %d", "The value is", 19) != buf) {
  1693.     error("vsprintf error 1\n");
  1694.     }
  1695.     if (strcmp(buf, "The value is 19") != 0) {
  1696.     error("vsprintf error 2\n");
  1697.     }
  1698. }
  1699. @
  1700.  
  1701.  
  1702. 1.2
  1703. log
  1704. @Disable some tests that either sprited or Mach can't support.
  1705. @
  1706. text
  1707. @d20 1
  1708. a20 1
  1709. static char rcsid[] = "$Header: /sprite/src/tests/stdio/RCS/stdio.c,v 1.11 89/03/16 11:54:28 ouster Exp $ SPRITE (Berkeley)";
  1710. a524 6
  1711.     /* 
  1712.      * These tests will fail as long as stdout et al aren't backed by real 
  1713.      * files. 
  1714.      */
  1715. #ifdef LIBC_USECONSOLE
  1716.  
  1717. a604 2
  1718. #endif /* LIBC_USECONSOLE */
  1719.  
  1720. a1614 1
  1721. #ifdef LIBC_USECONSOLE
  1722. a1649 1
  1723. #endif /* LIBC_USECONSOLE */
  1724. @
  1725.  
  1726.  
  1727. 1.1
  1728. log
  1729. @Initial revision
  1730. @
  1731. text
  1732. @d20 1
  1733. a20 1
  1734. static char rcsid[] = "$Header: /sprite/src/tests/stdio/RCS/stdio.c,v 1.12 91/12/12 13:43:34 shirriff Exp $ SPRITE (Berkeley)";
  1735. d525 6
  1736. d611 2
  1737. d1274 1
  1738. d1304 1
  1739. d1553 6
  1740. d1564 1
  1741. d1623 1
  1742. d1659 1
  1743. @
  1744.